/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

public class MapEditor extends GUI {
	private JFileChooser fileChooser = new JFileChooser();
	private JMenuItem addPiece, modifyPiece, removePiece, modifyBoard;
	private MapEditor me = this;
	
	private ActionListener menuListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(addPiece.getActionCommand())) {
				Piece newPiece = new Piece(getBoardWidget().getFocusedSquare().getLocation(), 1, getBoardWidget().getBoard().getPlayers().get(0));
				Piece resultPiece = PieceModifier.showDialog(me, "New Piece", newPiece, getBoardWidget().getBoard().getPlayers());
				
				if (resultPiece != null) {
					getBoardWidget().getBoard().addPiece(resultPiece);
					select(null);
				}
			}
			else if (e.getActionCommand().equals(modifyPiece.getActionCommand())) {
				if (isSelectedPiece()) {
					Piece cachedPiece = getSelectedPiece();
					Piece newPiece = new Piece(cachedPiece.getLocation(), cachedPiece.getLevel(), cachedPiece.getOwner());
					Piece resultPiece = PieceModifier.showDialog(me, "Edit Piece", newPiece, getBoardWidget().getBoard().getPlayers());
					
					if (resultPiece != null) {
						getBoardWidget().getBoard().setLocation(cachedPiece, resultPiece.getLocation());
						getBoardWidget().getBoard().setLevel(cachedPiece, resultPiece.getLevel());
						getBoardWidget().getBoard().setOwner(cachedPiece, resultPiece.getOwner());
						select(null);
					}					
				}
			}
			else if (e.getActionCommand().equals(removePiece.getActionCommand())) {
				if (isSelectedPiece()) {
					getBoardWidget().getBoard().remove(getSelectedPiece());
					select(null);
					refresh();
				}
			}
			else if (e.getActionCommand().equals(modifyBoard.getActionCommand())) {
				Board cachedBoard = getBoardWidget().getBoard();
				Board newBoard = new Board(cachedBoard.getCurrentPlayer(), cachedBoard.getSize(), cachedBoard.getPieces());
				Board resultBoard = BoardModifier.showDialog(me, "Edit Board", newBoard);
				
				if (resultBoard != null) {
					cachedBoard.setCurrentPlayer(resultBoard.getCurrentPlayer());
					getBoardWidget().setSize(resultBoard.getSize());
					cachedBoard.setPieces(resultBoard.getPieces());
					refresh();
				}
			}
		}
	};
	
	private void initializeWindow() {
		this.addPiece = new JMenuItem("Add Piece");
		this.modifyPiece = new JMenuItem("Modify Piece");
		this.removePiece = new JMenuItem("Remove Piece");
		this.modifyBoard = new JMenuItem("Modify Board");
		
		this.addPiece.setActionCommand("Add Piece");
		this.modifyPiece.setActionCommand("Modify Piece");
		this.removePiece.setActionCommand("Remove Piece");
		this.modifyBoard.setActionCommand("Modify Board");
		
		this.addPiece.addActionListener(this.menuListener);
		this.modifyPiece.addActionListener(this.menuListener);
		this.removePiece.addActionListener(this.menuListener);
		this.modifyBoard.addActionListener(this.menuListener);
		
		super.editMenu.add(this.addPiece);
		super.editMenu.add(this.modifyPiece);
		super.editMenu.add(this.removePiece);
		super.editMenu.add(this.modifyBoard);
		
		this.addPiece.setMnemonic(KeyEvent.VK_A);
		this.modifyPiece.setMnemonic(KeyEvent.VK_M);
		this.removePiece.setMnemonic(KeyEvent.VK_R);
		this.modifyBoard.setMnemonic(KeyEvent.VK_B);
		
		this.modifyPiece.setEnabled(isSelectedPiece());
		this.removePiece.setEnabled(isSelectedPiece());
		
		this.setTitle("Stones Map Editor");
		this.setVisible(true);
	}
	
	public MapEditor() {
		super();
		this.initializeWindow();
		this.newGame();
	}
	
	
	public MapEditor(Board board) {
		super();
		this.initializeWindow();
		this.setBoardWidget(new BoardWidget(board));
	}
	
	@Override
	protected void move(Point location) {
		if (!this.getBoardWidget().getBoard().isEmpty(location)) {
			this.getBoardWidget().getBoard().remove(this.getBoardWidget().getBoard().getPiece(location));
		}
		
		if (this.isSelectedPiece()) {
			this.getBoardWidget().getBoard().setLocation(this.getSelectedPiece(), location);
			this.getBoardWidget().setFocusedSquare(location);
			this.getBoardWidget().setHightlightTargets(null);
			this.getBoardWidget().setSelectedSquare(null);
			this.setSelectedPiece(null);
		}
		
		this.refresh();
	}
	
	@Override
	protected void select(Square square) {
		if (square != null) {
			this.getBoardWidget().setFocusedSquare(square.getLocation());
			this.setSelectedPiece(square.getPiece());
			
			if (this.isSelectedPiece()) {
				this.getBoardWidget().setSelectedSquare(square.getLocation());
			}
			else {
				this.getBoardWidget().setSelectedSquare(null);
			}
		}
		else {
			this.getBoardWidget().setSelectedSquare(null);
			this.setSelectedPiece(null);
		}
		
		this.modifyPiece.setEnabled(isSelectedPiece());
		this.removePiece.setEnabled(isSelectedPiece());
		
		this.refresh();
	}
	
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) {}
		
		new MapEditor();
	}
	
	@Override
	protected void newGame() {
		try {
			File templateFile = new File(this.getTemplateFolder() + this.getFileSeparator() + "2x12_8x8.xml");
			
			if (!templateFile.exists()) {
				GameLoader gameLoader = new GameLoader(GamePlayer.getInputStream("templates/2x12_8x8.xml"));
				
				new GameSaver(gameLoader.getEngine().getBoard(), templateFile);
			}
			
			GameLoader gameLoader = new GameLoader(templateFile);
			
			this.setBoardWidget(new BoardWidget(gameLoader.getEngine().getBoard()));
		} catch (Exception e) {
			this.displayMessage(e.getMessage());
		}
	}
	
	@Override
	protected void open() {
		int result = this.fileChooser.showOpenDialog(this);
		
		if (result == JFileChooser.APPROVE_OPTION) {
			GameLoader gameLoader = new GameLoader(this.fileChooser.getSelectedFile());
			
			this.setBoardWidget(new BoardWidget(gameLoader.getEngine().getBoard()));
		}
	}
	
	@Override
	protected void save() {
		if (this.getBoardWidget() != null) {
			int result = this.fileChooser.showSaveDialog(this);
			
			if (result == JFileChooser.APPROVE_OPTION) {
				new GameSaver(this.getBoardWidget().getBoard(), this.fileChooser.getSelectedFile());
			}
		}
		else {
			this.displayMessage("There is no game to save!");
		}
	}
	
	@Override
	protected boolean isSelectedPiece() {
		return this.getSelectedPiece() != null;
	}
}

